package com.cn.kabalifeuser.ui.service.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;

import com.cn.kabalifeuser.R;
import com.cn.kabalifeuser.base.BasePresenter;
import com.cn.kabalifeuser.base.IBaseActivity;
import com.cn.kabalifeuser.utils.LoggerUtils;
import com.cn.kabalifeuser.utils.X5WebView;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import butterknife.BindView;

/**
 * Created by 晓林 on 2020/9/14.
 * function: 链接
 */
public class RichTextWebViewH5Activity extends IBaseActivity {
    @BindView(R.id.webView)
    X5WebView webView;

    Bundle bundle;
    String url;
    String title;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_webview;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);

        bundle = getIntent().getExtras();
        url = bundle.getString("url");
//        title = bundle.getString("title");
        setTitleBar("");

        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);//允许使用js
        //不支持屏幕缩放
        webSettings.setSupportZoom(false);
        webSettings.setBuiltInZoomControls(false);
        //不显示webview缩放按钮
        webSettings.setDisplayZoomControls(false);
        LoggerUtils.e("url:" + url);


//        webView.setWebViewClient(new WebViewClient() {
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView webView, String url) {
//                String urlStr = url;
//                LoggerUtils.i("=====查看链接url:" + urlStr);
//                if (url == null) {
//                    return false;
//                }
//                try {
//                    if (url.startsWith("weixin://") || url.startsWith("alipays://") ||
//                            url.startsWith("tel://")) {
//                        //类型我目前用到的是微信、支付宝、拨号 三种跳转方式，其他类型自加
//                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
//                        startActivity(intent);
//                        return true;
//                    }
//                } catch (Exception e) {
//                    return false;
//                }
//                webView.loadUrl(url);
//                return true;
//            }
//        });
        webView.loadUrl(url);

//        if (!TextUtils.isEmpty(bean.getInfo().getContent())) {
//            StringBuilder sb = new StringBuilder();
//            sb.append(WebViewHtmlUtils.getHtmlData(bean.getInfo().getContent()));
//            webView.loadDataWithBaseURL(null, sb.toString(), "text/html", "utf-8", null);//加载html数据
//        }

//        webView.setWebChromeClient(new WebChromeClient() {
//            @Override
//            public void onReceivedTitle(WebView view, String title) {
//                super.onReceivedTitle(view, title);
//                if (title != null) {
//                    top_title.setText(title);
//                }
//            }
//        });
    }

//    @SuppressLint("SetJavaScriptEnabled")
//    @Override
//    public void onResume() {
//        super.onResume();
//        webView.onResume();
//        webView.getSettings().setJavaScriptEnabled(true);
//    }
//
//    @Override
//    public void onPause() {
//        super.onPause();
//        webView.onPause();
//        webView.getSettings().setLightTouchEnabled(false);
//    }

    //销毁 放置内存泄漏
    @Override
    public void onDestroy() {
        if (this.webView != null) {
            webView.destroy();
        }
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (webView != null && webView.canGoBack()) {
                webView.goBack();
                return true;
            } else
                return super.onKeyDown(keyCode, event);
        }


        return super.onKeyDown(keyCode, event);
    }

}
